package com.intellagric.service.iftmonitor.Impl.ift_equipment;

import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.pojo.ResponseMessage;
import com.intellagric.mapper.IftEquipmentTypeMapper;
import com.intellagric.pojo.IftEquipmentType;
import com.intellagric.pojo.IftEquipmentTypeExample;
import com.intellagric.service.iftmonitor.ift_equipment.EquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zhy
 * @email: 1269679946@qq.com
 * @Date: 2018/10/20
 * @DESC： 设备分类类型管理服务接口
 */
@Service
public class EquipmentTypeServiceImpl implements EquipmentTypeService {

    @Autowired
    private IftEquipmentTypeMapper equipmentTypeMapper;

    /**
     * 添加设备分类
     * @param equipmentType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage addEquipmentType(IftEquipmentType equipmentType) {
        if (equipmentTypeMapper.insertSelective(equipmentType) == 1 ) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 删除设备分类信息
     * @param id
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage deleteEquipmentType(String id) {
        if (equipmentTypeMapper.deleteByPrimaryKey(id) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 修改设备分类信息
     * @param equipmentType
     * @return ResponseMessage
     */
    @Override
    public ResponseMessage editEquipmentType(IftEquipmentType equipmentType) {
        if (equipmentTypeMapper.updateByPrimaryKeySelective(equipmentType) == 1) {
            return ResponseMessage.success();
        } else {
            return ResponseMessage.fail();
        }
    }

    /**
     * 查询设备分类详细信息
     * @param id
     * @return IftEquipmentType
     */
    @Override
    public IftEquipmentType getEquipmentTypeById(String id) {
        return equipmentTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有设备分类信息列表
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentTypeList() {
        IftEquipmentTypeExample equipmentTypeExample = new IftEquipmentTypeExample();
        equipmentTypeExample.setOrderByClause("sort");//升序排列
        equipmentTypeExample.createCriteria().andIdIsNotNull();
        List<IftEquipmentType> equipmentTypeList = equipmentTypeMapper.selectByExample(equipmentTypeExample);
        return LayuiDataGridResult.success().add(equipmentTypeList,equipmentTypeList.size());
    }

    /**
     * 通过parentId查询分类列表，实现异步查询
     * @param parentId
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentTypeByPid(String parentId) {
        IftEquipmentTypeExample equipmentTypeExample = new IftEquipmentTypeExample();
        equipmentTypeExample.setOrderByClause("sort");//升序排列
        equipmentTypeExample.createCriteria().andParentIdEqualTo(parentId);
        List<IftEquipmentType> equipmentTypeList = equipmentTypeMapper.selectByExample(equipmentTypeExample);
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(equipmentTypeList);
        return result;
    }

    /**
     * 查询分类信息，并生成树形数据
     * @return LayuiDataGridResult
     */
    @Override
    public LayuiDataGridResult getEquipmentTypeTree() {
        //查询所有分类列表
        List<IftEquipmentType> equipmentTypeList = this.getEquipmentTypeList().getData();

        //获取顶级分类列表
        List<IftEquipmentType> rootEquipmentList = this.getEquipmentTypeByPid("0").getData();

        //开始遍历查询父分类的子列表
        for(IftEquipmentType equipmentType : rootEquipmentList) {
            equipmentType.setChildren(getChild(equipmentType.getId(),equipmentTypeList));
        }
        LayuiDataGridResult result = new LayuiDataGridResult();
        result.setData(rootEquipmentList);
        return result;
    }

    private List<IftEquipmentType> getChild(String id, List<IftEquipmentType> equipmentTypeList) {
        // 子菜单
        List<IftEquipmentType> childList = new ArrayList<>();

        // 遍历所有节点，将父菜单id与传过来的id比较
        for (IftEquipmentType equipmentType : equipmentTypeList) {
            if (equipmentType.getParentId() != null) {   //所有非以及菜单的都要加入chidList中（递归）
                if (equipmentType.getParentId().equals(id)) {
                    childList.add(equipmentType);
                }
            }
        }

        // 把子菜单的子菜单再循环一遍
        for (IftEquipmentType equipmentType : childList) {// 没有url子菜单还有子菜单（暂不考虑url的问题）
            // 递归
            equipmentType.setChildren(getChild(equipmentType.getId(), equipmentTypeList));
        }
        // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }
}
